package cn.seapon.talker.ui;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CustomDialog extends Dialog {

	public CustomDialog(Context context) {
		super(context);
	}
	public CustomDialog(Context context, int theme) {
		super(context, theme);
	}

	public static class Builder {
		private Context context;
		
//		private String title = "";
		private String message = "";
		private String positiveButtonText;
		private String ignoreButtonText;
		private String negativeButtonText;
		private View contentView;
		
		private DialogInterface.OnClickListener positiveButtonClickListener;
		private DialogInterface.OnClickListener ignoreButtOnClickListener;
		private DialogInterface.OnClickListener negativeButtonClickListener;

		public Builder(Context context) {
			this.context = context;
		}

		public Builder setMessage(String message) {
			this.message = message;
			return this;
		}

		/**
		 * Set the Dialog message from resource
		 * 
		 * @param title
		 * @return
		 */
		public Builder setMessage(int message) {
			this.message = (String) context.getText(message);
			return this;
		}

		/**
		 * 设置标题
		 * @param title
		 * @return
		 */
//		public Builder setTitle(int title) {
//			this.title = (String) context.getText(title);
//			return this;
//		}
//		public Builder setTitle(String title) {
//			this.title = title;
//			return this;
//		}

		public Builder setContentView(View v) {
			this.contentView = v;
			return this;
		}


		/**
		 * 设置取消按钮
		 * @param positiveButtonText
		 * @param listener
		 * @return
		 */
		public Builder setPositiveButton(int positiveButtonText,
				DialogInterface.OnClickListener listener) {
			this.positiveButtonText = (String) context
					.getText(positiveButtonText);
			this.positiveButtonClickListener = listener;
			return this;
		}
		public Builder setPositiveButton(String positiveButtonText,
				DialogInterface.OnClickListener listener) {
			this.positiveButtonText = positiveButtonText;
			this.positiveButtonClickListener = listener;
			return this;
		}
		
		/**
		 * 设置忽略按钮
		 * @param ignoreButtonText
		 * @param listener
		 * @return
		 */
		public Builder setIgnoreButton(int ignoreButtonText,
				DialogInterface.OnClickListener listener){
			this.ignoreButtonText = (String) context.getText(ignoreButtonText);
			this.ignoreButtOnClickListener = listener;
			return this;
		}
		public Builder setIgnoreButton(String ignoreButtonText,
				DialogInterface.OnClickListener listener) {
			this.ignoreButtonText = ignoreButtonText;
			this.ignoreButtOnClickListener = listener;
			return this;
		}

		/**
		 * 设置积极的按钮
		 * @param negativeButtonText
		 * @param listener
		 * @return
		 */
		public Builder setNegativeButton(int negativeButtonText,
				DialogInterface.OnClickListener listener) {
			this.negativeButtonText = (String) context
					.getText(negativeButtonText);
			this.negativeButtonClickListener = listener;
			return this;
		}
		public Builder setNegativeButton(String negativeButtonText,
				DialogInterface.OnClickListener listener) {
			this.negativeButtonText = negativeButtonText;
			this.negativeButtonClickListener = listener;
			return this;
		}

		
		public CustomDialog create() {
			//获得系统的布局填充器
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			//填充自定义布局
			final CustomDialog dialog = new CustomDialog(context,R.style.Dialog);
			View layout = inflater.inflate(R.layout.dialog_normal_layout, null);
			//布局设置，width match_parent     height wrap_content
			dialog.addContentView(layout, 
					new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			//标题设置
//			((TextView) layout.findViewById(R.id.title)).setText(title);
			
			//设置积极的按钮
			if (positiveButtonText != null) {
				((Button) layout.findViewById(R.id.positiveButton)).setText(positiveButtonText);
				if (positiveButtonClickListener != null) {
					((Button) layout.findViewById(R.id.positiveButton)).setOnClickListener(
							new View.OnClickListener() {
								public void onClick(View v) {
									positiveButtonClickListener.onClick(dialog,DialogInterface.BUTTON_POSITIVE);
								}
							});
				}
			} else {
				// 如果没有文本则设置为隐藏
				layout.findViewById(R.id.positiveButton).setVisibility(View.GONE);
			}
			// 设置中立/忽略按钮
			if (ignoreButtonText != null) {
				((Button) layout.findViewById(R.id.ignoreButton)).setText(ignoreButtonText);
				if (ignoreButtOnClickListener != null) {
					((Button) layout.findViewById(R.id.ignoreButton)).setOnClickListener(new View.OnClickListener() {
								public void onClick(View v) {
									ignoreButtOnClickListener.onClick(dialog,DialogInterface.BUTTON_NEUTRAL);//中立
								}
							});
				}
			} else {
				layout.findViewById(R.id.ignoreButton).setVisibility(View.GONE);
			}
			// 设置取消按钮
			if (negativeButtonText != null) {
				((Button) layout.findViewById(R.id.negativeButton)).setText(negativeButtonText);
				if (negativeButtonClickListener != null) {
					((Button) layout.findViewById(R.id.negativeButton)).setOnClickListener(new View.OnClickListener() {
								public void onClick(View v) {
									negativeButtonClickListener.onClick(dialog,DialogInterface.BUTTON_NEGATIVE);
								}
							});
				}
			} else {
				layout.findViewById(R.id.negativeButton).setVisibility(View.GONE);
			}
			
			// set the cancel button
			if (negativeButtonText != null) {
				((Button) layout.findViewById(R.id.negativeButton)).setText(negativeButtonText);
				if (negativeButtonClickListener != null) {
					((Button) layout.findViewById(R.id.negativeButton)).setOnClickListener(new View.OnClickListener() {
								public void onClick(View v) {
									negativeButtonClickListener.onClick(dialog,DialogInterface.BUTTON_NEGATIVE);
								}
							});
				}
			} else {
				layout.findViewById(R.id.negativeButton).setVisibility(View.GONE);
			}
			
			
			// 设置内容部分
			if (message != null) {
				((TextView) layout.findViewById(R.id.message)).setText(message);
			} else if (contentView != null) {//藏下视图
				((LinearLayout) layout.findViewById(R.id.message))
						.removeAllViews();
				((LinearLayout) layout.findViewById(R.id.message)).addView(
						contentView, new LayoutParams(
								LayoutParams.WRAP_CONTENT,
								LayoutParams.WRAP_CONTENT));
			}
			dialog.setContentView(layout);
			return dialog;
		}

	}
}
